/*
 * @(#)UserpickerTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.taglib.html;

import java.sql.Connection;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.struts.util.RequestUtils;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.WebUtil;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.dao.UserRecordDAObject;

/**
    UserpickerTag.java

    <p>This class is to extend the TextTag provided by Apache Struts.<p>
    <p>Modification in "struts-html.tld"<br>

  <pre>
     [ADDED]
     &lt;tag&gt;
     &lt;name&gt;userpicker&lt;/name&gt;
     &lt;tagclass&gt;com.dcivision.framework.taglib.html.UserpickerTag&lt;/tagclass&gt;
     &lt;bodycontent&gt;empty&lt;/bodycontent&gt;
     &lt;attribute&gt;
     &lt;name&gt;accesskey&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;alt&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;altKey&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;disabled&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;indexed&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;maxlength&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;name&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onblur&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onchange&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onclick&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;ondblclick&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onfocus&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onkeydown&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onkeypress&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onkeyup&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onmousedown&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onmousemove&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onmouseout&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onmouseover&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onmouseup&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;property&lt;/name&gt;
     &lt;required&gt;true&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;readonly&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;size&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;style&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;styleClass&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;styleId&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;tabindex&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;title&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;titleKey&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;value&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;textOnly&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;/tag&gt;
     [ADDED]
  </pre>
  </p>

  <p>USAGE:</p>
  <p>If textOnly is set to true, only the full name of the user record, which id is the value of the hidden field, is shown. </p>
  <p>Otherwise there is an icon for poping up a box showing all user records, from which user can select a user record.</p>
  <p><code>&lt;html:userpicker property='reportToStaffRecordID' textOnly="true" /&gt;</code></p>

  @author          Wong Yam Lee
  @company         DCIVision Limited
  @creation date   12/08/2003
  @version         $Revision: 1.20.6.5 $
 */
public class UserpickerTag extends TextTag {

  public static final String REVISION = "$Revision: 1.20.6.5 $";

  private String pageFlag = "";
  private String isMulitple = "";
  private String mulitpleValue = "";
  private String formName = "";
  private Integer index = null;

  private String grantShowAdmin= "";

  public String getGrantShowAdmin() {
  return grantShowAdmin;
  }

  public void setGrantShowAdmin(String grantShowAdmin) {
  this.grantShowAdmin = grantShowAdmin;
  }

  public String getPageFlag() {
  return pageFlag;
  }

  public void setPageFlag(String pageFlag) {
  this.pageFlag = pageFlag;
  }

  public String getIsMulitple() {
  return isMulitple;
  }

  public void setIsMulitple(String isMulitple) {
  this.isMulitple = isMulitple;
  }

  public String getMulitpleValue() {
  return mulitpleValue;
  }

  public void setMulitpleValue(String mulitpleValue) {
  this.mulitpleValue = mulitpleValue;
  }


  public Integer getIndex() {
    return index;
  }

  public void setIndex(Integer index) {
    this.index = index;
  }

  public String getFormName() {
    return formName;
  }

  public void setFormName(String formName) {
    this.formName = formName;
  }

  /** default constructor
   */
  public UserpickerTag() {
    super();
  }

  public int doStartTag() throws JspException {
    FormTag formTag = (FormTag)BodyTagSupport.findAncestorWithClass(this, FormTag.class);
    Object valueObj = RequestUtils.lookup(pageContext, name, property, null);
    String value = null;
    boolean str=true;
    if(valueObj instanceof String[]){

      if (valueObj==null) {
        value = "";
      //} else if (index.intValue() >= ((String[])valueObj).length) {
      //  value = "";
      } else {
        value = ((String[])valueObj)[0];
      }
      str=false;
    } else {
      value = valueObj==null? "": valueObj.toString();
    }
    String sPropertyName = this.getProperty();
    int retValue;

    // set the display value
    if ( Utility.isEmpty(value) ) {
      if("listDmsAuditTrail".equals(this.getPageFlag())||"listDmsIndividualAuditTrail".equals(this.getPageFlag())){
        setValue("All User");
      }else{
          setValue("");
      }
    }else{
      try {
      if(value.indexOf(",")>-1){
        String[] splitAry = TextUtility.splitString(value,",");
        String showValue = "";
        for(int i=0; i<splitAry.length; i++){
        if(i!=0){
          showValue += ",";
        }
          UserRecord record = getUserRecordByID(new Integer(splitAry[i]));
            if(GlobalConstant.RECORD_STATUS_ACTIVE.equals(record.getRecordStatus())){
              showValue += record.getFullName();
            }else{
              showValue += "<s>"+record.getFullName()+"</s>";
            }
        }
        setValue(showValue);
      }else{
          UserRecord record = getUserRecordByID(new Integer(value));
          if(GlobalConstant.RECORD_STATUS_ACTIVE.equals(record.getRecordStatus())){
            setValue(record.getFullName());
          }else{
            setValue("<s>"+record.getFullName()+"</s>");
          }
        }
      } catch (ApplicationException appEx) {
        throw new JspException(appEx);
      }
    }
    if(!Utility.isEmpty(index)){
      setProperty("dummy_" + sPropertyName + "_" + index);
    }else{
      setProperty("dummy_" + sPropertyName );
    }
    setOnkeypress("return false;");   // disable keyboard input
    setOnkeydown("return false;");    // disable keyboard input
    retValue = super.doStartTag();
    setProperty(sPropertyName);

    try{
      JspWriter out = pageContext.getOut();

      // hidden field
      if(!Utility.isEmpty(index)){
        out.println("<input type='hidden' name='" + sPropertyName + "_" + index + "' value='" + value + "'>");
      }else{
        out.println("<input type='hidden' name='" + sPropertyName  + "' value='" + value + "'>");
      }
      if(Utility.isEmpty(this.getFormName()) && formTag!=null){
        formName = formTag.getBeanName();
      }
      // show the user picker if it is for editing
      if (!textOnly && !(formTag!=null && formTag.getTextOnly())) {
        String contextPath = (String)this.pageContext.getServletContext().getAttribute(GlobalConstant.CONTEXT_PATH_KEY);
        String sUrl ="";

        sUrl = contextPath + "/user/PopupUserRecord.do?mode=L&property=" + sPropertyName + "&form=" + formName+"&pageFlag=" + this.getPageFlag()+"&isMulitple=" + this.getIsMulitple()+"&mulitpleValue=" + this.getMulitpleValue()+"&index="+this.index;
//        if(!str){
//        contextPath=contextPath;
//        }
        //bruce.ma
       if(!Utility.isEmpty(this.getGrantShowAdmin())){
          sUrl = sUrl + "&grantShowAdmin=" + this.getGrantShowAdmin();

        }

        String sName = "DCIVisionPopupUser";
        String sPopW = "700";
        String sPopH = "550";
        String sFeature = "scrollbars=yes,resizable=yes";

        // <a> tag for displaying the user picker
        out.print(" <a href=\"javascript:void(0);\" onclick=\"return OpenCenteredPopup('" + sUrl + "', '" + sName + "', " + sPopW + ", " + sPopH + ", '" + sFeature + "');\">");

        //print the html img tag for search user image
        out.print("<img ");
        out.print("src=\""+WebUtil.getImage(pageContext.getRequest(), "IconSearchUser.gif")+"\" ");
        out.print("align=\"absmiddle\" ");
        out.print("border=\"0\" ");
        out.print(">");

        // close the <a> tag
        out.println("</a>");
      }
    } catch (Exception e) {
      throw new JspException(e);
    }
    return retValue;
  }

  /** return a user record according to the given id
   *  @param    id
   *  @return   UserRecord
   *  @throws   ApplicationException
   */
  protected UserRecord getUserRecordByID(Integer id) throws ApplicationException {
    SessionContainer sessionCon = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    Connection conn = (Connection)this.pageContext.getRequest().getAttribute(GlobalConstant.DB_KEY);
    UserRecordDAObject dao = new UserRecordDAObject(sessionCon, conn);

    try {
      return (UserRecord)dao.getUserByID(id);
    } catch (ApplicationException appEx) {
      throw appEx;
    }
  }

}
